package com.topisv.tms.tools;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.Validate;

import com.topisv.tms.entity.CustomerLead;
import com.topisv.tms.entity.JSONParam;

/**
 * Class: dataTables工具类
 * @author chenhong
 */
public class DataTablesUtils {
	
	/**
	 * 将客户端JSONParm格式的参数，转换为map格式
	 * @param params
	 * @return
	 */
	public static HashMap<String,String> convertToMap(JSONParam[] params){
		HashMap<String, String> map=new HashMap<String, String>();
		for (JSONParam p : params) {
			map.put(p.getName(), p.getValue());
		}
		return map;
	}
	
	public static Map<String, Object> getParametersStartingWith(Map<String, String> request, String prefix) {
		Validate.notNull(request, "Request must not be null", new Object[0]);
		Map<String, Object> params = new TreeMap<String, Object>();
		if (prefix == null)
			prefix = "";
		for (Entry<String, String> paramNames : request.entrySet()) {
			String paramName = paramNames.getKey();
			if ("".equals(prefix) || paramName.startsWith(prefix)) {
				String unprefixed = paramName.substring(prefix.length());
				String value = paramNames.getValue();
				params.put(unprefixed, value);
			}
		}
		return params;
	}
	
	public static Map<String,Object> getParamerFromJsonRequest(Map<String, Object> request){
		Validate.notNull(request, "Request must not be null", new Object[0]);
		Map<String, Object> params = new TreeMap<String, Object>();
		for (Entry<String, Object> paramNames : request.entrySet()) {
			String paramName = paramNames.getKey();
			String unprefixed = paramName.substring(paramName.lastIndexOf("_")+1);
			Object value = paramNames.getValue();
			params.put(unprefixed, value);
		}
		return params;
	}
	
   /**
    * 将数据定义的表头信息，转换成数据列表
    * @param lead
    * @return [参数说明]
    * 
    * @return List<List<String[]>> 多sheet，多表头
    * @exception throws [违例类型] [违例说明]
    * @see [类、类#方法、类#成员]
    */
    public static List<List<String[]>> getXlsHeads(CustomerLead lead) {
        List<List<String[]>> heads = null;
        if (null != lead && StringUtils.isNotEmpty(lead.getParams())) {
            // 分多sheet
            String[] headBody = lead.getParams().split(";");
            List<String[]> rowList = null;
            
            for (String body : headBody) {
                heads = new ArrayList<List<String[]>>();
                // 分多行
                String[] rows = body.split(":");
                rowList = new ArrayList<String[]>(rows.length);
                for (String row : rows) {
                    // 放入多列
                    rowList.add(row.split(","));
                }
                heads.add(rowList);
            }
        }
        return heads;
    }
    
    /**
     * 解析数据库存储的sheel表格的模板。<br/>
     * 后期建议用json等对象存储在数据库，便于转换
     * @author zhangpeijun
     */
    public static List<TablePojo> getTablePojo(CustomerLead lead) {
        List<TablePojo> tableList = null;
        if (null != lead && StringUtils.isNotEmpty(lead.getParams())) {
            // 分多sheet
            String[] headBody = lead.getParams().split(";");
            // split分割后，不需要去考虑 null的情况,但" ".split 出来的数组长度为0
            if (headBody.length > 0) {
                tableList = new ArrayList<TablePojo>(headBody.length);
                TablePojo table = null;
                List<String[]> rowList = null;
                String[] rows = null;
                for (String body : headBody) {
                    table = new TablePojo();
                    String[] sheet = body.split("\\|");
                    if (1 == sheet.length) {
                        // 如果没有分隔符，默认当作表头处理。
                        rows = sheet[0].split(":");
                    } else if (1 < sheet.length) {
                        // 设置表名
                        table.setName(sheet[0]);
                        // 分多行
                        rows = sheet[1].split(":");
                    }
                    rowList = new ArrayList<String[]>(rows.length);
                    for (String row : rows) {
                        // 放入多列
                        rowList.add(row.split(","));
                    }
                    table.setHeads(rowList);
                    tableList.add(table);
                }
            }
        }
        return tableList;
    }
}
